package com.haojin.spring.resources;

import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author .29.
 * @create 2023-03-02 20:20
 */

//测试ClassPathResource();
public class ClassPathResourceDemo {
    public static void main(String[] args){
        loadClassPathResource("baidu.txt");
    }
    public static void loadClassPathResource(String path){
        ClassPathResource resource = new ClassPathResource(path);

        //获取文件信息
        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());

        //获取文件内容
        try {
            InputStream inputStream = resource.getInputStream();
            byte[] bytes = new byte[1024];
            if(inputStream.read(bytes) != -1){
                System.out.println(new String(bytes));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
